<%@page import="cn.school.mybatis.pojo.Student"%>
<%@page import="java.util.UUID"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@ page language="java" import="java.io.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%-- <%
  request.setCharacterEncoding("utf-8");
 String sname=request.getParameter("sname");
 //long gradeid=Long.parseLong(request.getParameter("gradeid"));
 String gradeid=request.getParameter("gradeid");
 String idcard=request.getParameter("idcardpicture");
  out.print(sname+","+gradeid+","+idcard);
%> --%>
<%-- <%
  InputStream in=request.getInputStream();
  int ch=0;
  while((ch=in.read())>-1){
	  out.print((char)ch);
  }
%> --%>
<%
  Student stu=new Student();
  //stu.setSname(request.getParameter("sname"));
  FileItemFactory item=new DiskFileItemFactory();
  ServletFileUpload upload=new ServletFileUpload(item);
  upload.setHeaderEncoding("utf-8");//乱码
  List<FileItem> list=upload.parseRequest(request);
  
  for(FileItem fi:list){
	  if(fi.isFormField()){
		  //普通的表单元素
		  out.print("<h1>"+fi.getFieldName()+":"+fi.getString("utf-8")+"</h1>");
		  switch(fi.getFieldName()){
		  case "sname":
			  stu.setSname(fi.getString("utf-8"));
			  break;
		  case "gradeid":
			  stu.setGradeid(Integer.parseInt(fi.getString("utf-8")));
			  break;  
			  //...
		  }
	  }else{//文件
		  if(fi.getSize()==0)
			  continue;
		  out.print("<h1>这是一个文件,文件名:"+fi.getName()+",元素名:"+fi.getFieldName()+"</h1>");
		  String path=application.getRealPath("images");
		  //文件要换一个名字,保证后缀是相同
		  String fname=fi.getName();
		  String houzhui=fname.substring(fname.lastIndexOf('.'));
		  String fileName=UUID.randomUUID()+houzhui;
		  //stu.setIdcardpic("images/"+fileName);
		  stu.setIdcardpic(fileName);///!!!!!!!!!!!!
		  File f=new File(path,fileName);
		  
		  out.print("<img src='"+request.getContextPath()+"/images/"+fileName+"' height='300px' width='400px' />");
		  fi.write(f);
	  }
  }
%>
<hr/>
<%
  out.print("新生信息如下:<br/>"+stu);
%>
</body>
</html>